import sun.reflect.generics.tree.Tree;

public class BinarySearchTree {
    //内部内
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        //构造方法
        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;//根节点

    /**
     * 找val，返回结点的地址
     * 最好情况时间复杂度O(logN) 是一颗完全二叉树或者满二叉树的情况
     * @param val
     * @return
     */
    public TreeNode search(int val) {
        if(root == null) return null;
        TreeNode cur = root;

        while(cur != null) {
            if(cur.val < val) {
                cur = cur.right;
            }
            else if(cur.val > val) {
                cur = cur.left;
            }
            else {
                return cur;
            }
        }
        return null;
    }

    /**
     * 二叉受搜索树的插入
     */
    public boolean insert(int key) {
        //实例化一个结点
        TreeNode node = new TreeNode(key);
        if(root == null) {
            root = node;
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val == key) {
                //一样的值不能进行插入
                return false;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }

        if(parent.val > key) {
            parent.left = node;
        }else {
            parent.right = node;
        }
        return true;
    }

    /**
     * 删除结点k
     */
    public void removeNode(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else {
                remove(cur,parent);
                return;
            }
        }
    }

    /**
     * 删除cur结点
     * cur代表要删除的结点
     * parent代表要删除结点的父亲结点
     * @param cur
     * @param parent
     */
    private void remove(TreeNode cur, TreeNode parent) {
        //三种情况
        if(cur.left == null) {
            //三种情况
            if(cur == null) {
                root = cur.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == null) {
                root = cur.left;
            }else if(cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            //cur左右两边都不为空
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if (target == targetParent.left) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;
            }
        }
    }


}
